//
//  FristTuiJianTableViewController.m
//  开心一刻
//
//  Created by lanou on 16/4/7.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "FristTuiJianTableViewController.h"
#import "LYTuiJianCellTableViewCell.h"
#import "Requestmanager.h"
#import "LYTuiJianModel.h"
#import "playView.h"
#import "AppDelegate.h"
#import "MJRefresh.h"
#import "PlayViewController.h"
#import "LYWEbViewViewController.h"
@interface FristTuiJianTableViewController ()
@property (nonatomic,strong)NSMutableArray * array;
@property (nonatomic,strong)AVPlayer * player;
@property (nonatomic,strong)playView * myPlay;
@property (nonatomic,assign)CGFloat currenttIme;
@property (nonatomic,strong)NSString * urlString;
@property (nonatomic,strong)NSTimer * timer;
@property (nonatomic,strong)LYTuiJianCellTableViewCell * cellONe;
@property (nonatomic,strong)NSIndexPath * indexPath;
@property (nonatomic,strong)UIButton * bigButton;
@property (nonatomic,strong)UILabel * dissLable;
@property (nonatomic,strong)NSTimer * one;
@property (nonatomic,assign)NSInteger aa;
@end

@implementation FristTuiJianTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self request];
    _dissLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KedWidth, 30)];
    _dissLable.text = @"0_0 点击评论可以看到评论详情哟 亲 0_0";
    _dissLable.backgroundColor = [UIColor purpleColor];
    _dissLable.textAlignment =  NSTextAlignmentCenter ;
    [self.tableView addSubview:_dissLable];
    _aa = 1459928583;
    _one = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(dissMiss:) userInfo:nil repeats:NO];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self request1];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self downrequest];
    }];
    
}
#pragma mark --- 下刷新
- (void)downrequest
{
    _aa++;
    NSString * aaaa =@"http://ic.snssdk.com/neihan/stream/mix/v1/?content_type=-101&iid=4021588586&os_version=9.3&os_api=18&app_name=joke_essay&channel=App%20Store&device_platform=iphone&idfa=AEDD7D7A-5D81-4E83-8C22-11CFDEC78669&vid=CD14451C-2851-4DD0-BF51-D83E0C5079C7&openudid=f8e01dec4861c02b1a0182e67db467550a4c79ec&device_type=iPhone%206&version_code=5.0.1&ac=WIFI&screen_width=750&device_id=13441376529&aid=7&content_type=-101&count=30&essence=1&message_cursor=0&min_time=";
    
    NSString * str1 = [NSString stringWithFormat:@"%ld&mpic=1",_aa];
    
    NSString *str = [aaaa stringByAppendingString:str1];
   
    [Requestmanager requestWithUrl:str requesType:requestTypeGET parDic:nil finish:^(NSData *data) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
     NSMutableArray * arrry = [LYTuiJianModel getData:dic];
        for (LYTuiJianModel * modell in arrry)
        {
            [_array addObject:modell];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        [self.tableView.mj_footer endRefreshing];
    } error:^(NSError *error) {
        NSLog(@"caca");
    }];
    
    

    
}
#pragma mark =--= 刷新
- (void)request1
{
    _aa--;
    NSString * aaaa =@"http://ic.snssdk.com/neihan/stream/mix/v1/?content_type=-101&iid=4021588586&os_version=9.3&os_api=18&app_name=joke_essay&channel=App%20Store&device_platform=iphone&idfa=AEDD7D7A-5D81-4E83-8C22-11CFDEC78669&vid=CD14451C-2851-4DD0-BF51-D83E0C5079C7&openudid=f8e01dec4861c02b1a0182e67db467550a4c79ec&device_type=iPhone%206&version_code=5.0.1&ac=WIFI&screen_width=750&device_id=13441376529&aid=7&content_type=-101&count=30&essence=1&message_cursor=0&min_time=";
 
    NSString * str1 = [NSString stringWithFormat:@"%ld&mpic=1",_aa];
    
    NSString *str = [aaaa stringByAppendingString:str1];
   
    [Requestmanager requestWithUrl:str requesType:requestTypeGET parDic:nil finish:^(NSData *data) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        _array = [LYTuiJianModel getData:dic];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        [self.tableView.mj_header endRefreshing];
    } error:^(NSError *error) {
        NSLog(@"caca");
    }];
    

    
}
#pragma mark---提示lable消失
- (void)dissMiss:(NSTimer *)timer
{
    _dissLable.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark --- 返回分区

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}
#pragma mark  --- 返回行数 ---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _array.count;
}

#pragma mark--- 返回cell ---
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 LYTuiJianCellTableViewCell * cell = [[UINib nibWithNibName:@"LYTuiJIan" bundle:nil]instantiateWithOwner:self options:nil].firstObject;
    
  
    LYTuiJianModel * model = _array[indexPath.row];
        [cell  cellGetData:model];
    cell.selectionStyle =    UITableViewCellSelectionStyleNone;
    cell.button.tag = indexPath.row + 100;
    cell.pingLunBUtton.tag = indexPath.row + 200;
   [ cell.button  addTarget:self action:@selector(bbb:) forControlEvents:UIControlEventTouchUpInside];
    [cell.pingLunBUtton addTarget:self action:@selector(pingLunBUtton :) forControlEvents:UIControlEventTouchUpInside];
    [cell.juBaoBUtton addTarget:self action:@selector(juBaoBUtton:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
#pragma mark --- 评论跳转
- (void)pingLunBUtton: (UIButton *)button
{
    LYWEbViewViewController * lya = [[LYWEbViewViewController alloc]init];
    LYTuiJianModel * model = _array[button.tag - 200];
    lya.shareUrl = model.share_url;
    [_navc pushViewController:lya animated:YES];
}
#pragma mark --- 举报
- (void)juBaoBUtton:(UIButton *)button
{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请提交您的举报内容" preferredStyle:UIAlertControllerStyleAlert ];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }]];
   [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
       
   }];
    [self presentViewController:alert animated:NO completion:nil];
    
    
}
#pragma mark ---视频播放
- (void)bbb:(UIButton *)button
{
    [_myPlay.player pause];
    _cellONe = (LYTuiJianCellTableViewCell *)button.superview.superview.superview.superview;
    _indexPath = [self.tableView indexPathForCell:_cellONe];
    
    LYTuiJianModel * model = _array[button.tag - 100];
    _urlString = model.mp4_url;
    UIView * view = button.superview;
    _myPlay = [[playView alloc]initWithFrame:view.frame url:model.mp4_url];
    [view addSubview: _myPlay];
    
    
    _bigButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _bigButton .frame = CGRectMake(_myPlay.frame.size.width - 40,_myPlay.frame.size.height - 40, 40, 30);
    [_bigButton setTitle:@"全屏" forState:UIControlStateNormal];
    [_bigButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_myPlay addSubview:_bigButton];

    [self.bigButton addTarget:self action:@selector(bigMOvie:) forControlEvents:UIControlEventTouchUpInside];
   _timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(timer :) userInfo:nil repeats:YES];
    
}
#pragma mark ---界面出现
- (void)viewWillAppear:(BOOL)animated
{
    [self.tableView reloadData];
    [_timer setFireDate:[NSDate distantPast]];
}
#pragma mark --- 界面消失
- (void)viewWillDisappear:(BOOL)animated
{
    [_timer setFireDate:[NSDate distantFuture]];  
}
#pragma mark ===更新关闭AVplayer
- (void)timer: (NSTimer *)timer
{
   CGRect cellR = [self.tableView rectForRowAtIndexPath:_indexPath];
    if (self.tableView.contentOffset.y -cellR.origin.y <_cellONe.frame.size.height ||cellR.origin.y - self.tableView.contentOffset.y >self.tableView.frame.size.height)
    {
        
    }
    else
    {
     [_myPlay.player pause];
        
    }
    
}
#pragma mark --- 横屏
- (void)bigMOvie: (UIButton *)button
{
    _currenttIme = _myPlay.player.currentTime.value / _myPlay.player.currentTime.timescale;
    [_myPlay.player pause];
    AppDelegate * aaa = [UIApplication sharedApplication].delegate;
    aaa.isOk = YES;
    PlayViewController * play = [[PlayViewController alloc]init];
    play.string =_urlString;
    play.currTIme = _currenttIme;
    [self presentViewController:play animated:NO completion:nil];

    
}
#pragma mark --- 返回高度 ---
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
 
    LYTuiJianModel * model = _array[indexPath.row];
    return [LYTuiJianCellTableViewCell RowHeight:model];

}
#pragma mark --- 网络请求 ----
- (void)request
{
    
        [Requestmanager requestWithUrl:@"http://ic.snssdk.com/neihan/stream/mix/v1/?content_type=-101&iid=4021588586&os_version=9.3&os_api=18&app_name=joke_essay&channel=App%20Store&device_platform=iphone&idfa=AEDD7D7A-5D81-4E83-8C22-11CFDEC78669&vid=CD14451C-2851-4DD0-BF51-D83E0C5079C7&openudid=f8e01dec4861c02b1a0182e67db467550a4c79ec&device_type=iPhone%206&version_code=5.0.1&ac=WIFI&screen_width=750&device_id=13441376529&aid=7&content_type=-101&count=30&essence=1&message_cursor=0&min_time=1459928583&mpic=1" requesType:requestTypeGET parDic:nil finish:^(NSData *data) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
     
        _array = [LYTuiJianModel getData:dic];

        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } error:^(NSError *error) {
        NSLog(@"caca");
    }];
    
    
}
@end
